package com.junlong0716.enjoylearning.ui

import android.graphics.Color
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.blankj.utilcode.util.ToastUtils
import com.junlong0716.enjoylearning.R
import com.junlong0716.enjoylearning.adapter.ClassifyAdapter
import com.junlong0716.enjoylearning.constant.NetConstant.REQUEST_HEADER
import com.junlong0716.enjoylearning.dialog.LoadingDialog
import com.junlong0716.enjoylearning.entity.ClassifyEntity
import com.junlong0716.enjoylearning.net.api.NetApi.API_CLASSIFY
import com.junlong0716.getknowledgedemo.decoration.DividerItemNoEndDecoration
import com.lzy.okgo.OkGo
import com.lzy.okgo.cache.CacheMode
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity
import kotlinx.android.synthetic.main.activity_classify.*
import kotlinx.android.synthetic.main.view_title_bar.*
import org.json.JSONObject

class ClassifyActivity : RxAppCompatActivity(), View.OnClickListener {
    private lateinit var classifyData: ArrayList<ClassifyEntity>
    private lateinit var classifyAdapter: ClassifyAdapter
    private lateinit var loadingDialog: LoadingDialog
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_classify)
        rl_back.setOnClickListener(this)
        tv_title.text = "专项分类"
        loadingDialog = LoadingDialog(this)
        loadingDialog.setCanceledOnTouchOutside(false)
        loadingDialog.show()
        requestClassifyData()
        rv_list.layoutManager = LinearLayoutManager(this)
        classifyAdapter = ClassifyAdapter(classifyData, R.layout.item_classify)
        rv_list.adapter = classifyAdapter
        rv_list.addItemDecoration(DividerItemNoEndDecoration(this, DividerItemNoEndDecoration.VERTICAL_LIST, 20, Color.parseColor("#ededed")))
    }

    private fun requestClassifyData() {
        classifyData = ArrayList()
        OkGo.post<String>(REQUEST_HEADER + API_CLASSIFY)
                .tag(this)
                .cacheMode(CacheMode.REQUEST_FAILED_READ_CACHE)
                .cacheKey("CATCH_CLASSIFY")
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>?) {
                        loadingDialog.dismiss()
                        setData(response!!.body())
                    }

                    override fun onCacheSuccess(response: Response<String>?) {
                        loadingDialog.dismiss()
                        setData(response!!.body())
                    }

                    override fun onError(response: Response<String>?) {
                        ToastUtils.showShort("请求失败！")
                    }
                })
    }

    private fun setData(body: String?) {
        val jsonObject = JSONObject(body)
        val data = jsonObject.getJSONArray("data")
        val classifyData = ArrayList<ClassifyEntity>()
        for (i in 0 until data.length()) {
            val id = data.getJSONObject(i).getInt("id")
            val classifyName = data.getJSONObject(i).getString("classifyName")
            val details = data.getJSONObject(i).getJSONArray("details")
            val itemArray = ArrayList<ClassifyEntity.DetailsBean>()
            for (j in 0 until details.length()) {
                val id = details.getJSONObject(j).getInt("id")
                val classifyName = details.getJSONObject(j).getString("classifyName")
                itemArray.add(ClassifyEntity.DetailsBean(id, classifyName,false))
            }
            classifyData.add(ClassifyEntity(id, classifyName, itemArray))
        }
        this.classifyData.clear()
        this.classifyData.addAll(classifyData)
        classifyAdapter.notifyDataSetChanged()
    }

    override fun onClick(v: View?) {
        when (v!!.id) {
            R.id.rl_back -> {
                finish()
            }
            else -> {

            }
        }
    }
}
